/*******************************************************************************
 * Copyright (c) 2009, 2010 Torkild Ulvøy Resheim.
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Torkild Ulvøy Resheim - initial API and implementation
 *******************************************************************************/
package no.resheim.buildmonitor.ui.notifications;

import no.resheim.buildmonitor.core.BuildmonitorCore.ServerList;
import no.resheim.buildmonitor.core.data.IChangeReporter;
import no.resheim.buildmonitor.core.data.Job;
import no.resheim.buildmonitor.ui.BuildmonitorUI;

import org.eclipse.mylyn.internal.provisional.commons.ui.AbstractNotificationPopup;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

/**
 * Popup GUI that is used to notify about changes in the Buildmonitor data
 * model.
 * 
 * @author Torkild Ulvøy Resheim
 * @since 1.0
 */
@SuppressWarnings("restriction")
public class NotificationPopup extends AbstractNotificationPopup {

	private final IChangeReporter[] reporters;

	public NotificationPopup(Display display, IChangeReporter[] reporters) {
		super(display);
		setDelayClose(5000);
		setBlockOnOpen(true);
		setFadingEnabled(true);
		this.reporters = reporters;
		open();
	}

	@Override
	protected void createContentArea(Composite parent) {
		Composite c = new Composite(parent, SWT.NONE);
		c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		c.setLayout(new GridLayout(2, false));
		c.setSize(parent.getSize());
		for (IChangeReporter reporter : reporters) {
			Label image = new Label(c, SWT.NONE);
			if (reporter instanceof Job) {
				image.setImage(BuildmonitorUI.getDefault().getImageRegistry()
						.get(((Job) reporter).getHealth().name()));
			} else if (reporter instanceof ServerList) {
				image.setImage(BuildmonitorUI.getDefault().getImageRegistry()
						.get(BuildmonitorUI.IMG_SERVER));
			}
			Label label = new Label(c, SWT.WRAP);
			label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
			label.setText(reporter.getChangeDescription());
			label.setBackground(parent.getBackground());
		}
	}
}
